<?php

namespace App\Admin\Controllers\Service;

use App\Models\ServiceOrder;
use App\Models\User;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;

class OrderController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
    protected $title = '服务订单';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new ServiceOrder());

        $grid->column('id', ___('table.zy_service_order.Id'));
        $grid->column('order_sn', ___('table.zy_service_order.Order sn'));
        $grid->column('no', ___('table.zy_service_order.No'));
        $grid->column('service_snapshot_id', ___('table.zy_service_order.Service snapshot id'));
        $grid->column('user_id', ___('table.zy_service_order.User id'))->display(function ($id){
            return User::find($id)->name;
        });
        $grid->column('service_user_id', ___('table.zy_service_order.Service user id'))->display(function ($id){
            return User::find($id)->name;
        });

        $grid->column('status', ___('table.zy_service_order.Status'))->radio(ServiceOrder::$status);
        $grid->column('start_time', ___('table.zy_service_order.Start time'));
        $grid->column('end_time', ___('table.zy_service_order.End time'));
        $grid->column('create_time', ___('table.zy_service_order.Create time'));
        $grid->column('update_time', ___('table.zy_service_order.Update time'));

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {
        $show = new Show(ServiceOrder::findOrFail($id));

        $show->field('id', ___('table.zy_service_order.Id'));
        $show->field('order_sn', ___('table.zy_service_order.Order sn'));
        $show->field('no', ___('table.zy_service_order.No'));
        $show->field('service_snapshot_id', ___('table.zy_service_order.Service snapshot id'));
        $show->field('user_id', ___('table.zy_service_order.User id'));
        $show->field('service_user_id', ___('table.zy_service_order.Service user id'));
        $show->field('status', ___('table.zy_service_order.Status'));
        $show->field('start_time', ___('table.zy_service_order.Start time'));
        $show->field('end_time', ___('table.zy_service_order.End time'));
        $show->field('create_time', ___('table.zy_service_order.Create time'));
        $show->field('update_time', ___('table.zy_service_order.Update time'));

        return $show;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new ServiceOrder());

        $form->text('order_sn', ___('table.zy_service_order.Order sn'));
        /*$form->number('no', ___('table.zy_service_order.No'))->required()
            ->rules('nullable')
            ->help("涉及支付流水ID号");*/
        //$form->number('service_snapshot_id', ___('table.zy_service_order.Service snapshot id'));
        $form->select('user_id', ___('table.zy_service_order.User id'))->options(function ($id) {
            $user = User::find($id);
            if ($user) {
                return [$user->id => $user->name];
            }
        })->ajax('/admin/ajax/user')->help("输入真实用户姓名")->required();;
        $form->select('service_id', ___('table.zy_service_order.Service_id'))->options(function ($id) {
            $user = User::find($id);
            if ($user) {
                return [$user->id => $user->name];
            }
        })->ajax('/admin/ajax/service-list')->help("输入服务关键词");;

        //$form->text('pay_name', ___('table.zy_service_order.Pay name'));
        //$form->switch('pay_status', ___('table.zy_service_order.Pay status'));
        //$form->number('pay_time', ___('table.zy_service_order.Pay time'));
        //$form->switch('status', ___('table.zy_service_order.Status'));
        $form->datetime('start_time', ___('table.zy_service_order.Start time'))->format('YYYY-MM-DD HH:mm:ss');
        $form->datetime('end_time', ___('table.zy_service_order.End time'))->format('YYYY-MM-DD HH:mm:ss');
        //$form->number('create_time', ___('table.zy_service_order.Create time'));
        //$form->number('update_time', ___('table.zy_service_order.Update time'));

        return $form;
    }
}
